<?php

class Message{
	
	
	private static $logFile=null;
	
	
	private static $debugs=array();
	private static $infos=array();
	private static $warns=array();
	private static $errors=array();
	private static $crashs=array();
	


	public static function log($log, $type='TRACE'){
		if(self::$logFile==null){
			$date=date('Ymd');
			self::$logFile=FileSystem::createFile("data/logs/cinema-$date.log",false);
		}
		$date = date('d/m/Y H:i:s');
		$user = Security::getLogin();
                $file = $_SERVER['SCRIPT_NAME'];
		//on fait la conversion car le fichier n'est pas en UTF8 par défaut
		$trace="$type $date - $user - $file : ".iconv('UTF-8', 'Windows-1252', $log).PHP_EOL;
		error_log($trace, 3, self::$logFile);
	}
	
	public static function tryToLog($message,$level){
		$logLevel=Properties::getValue('logLevel');
		$needLog=false;
		switch($level){
			case 'CRASH' : if($logLevel=='crash') $needLog=true;
			case 'ERROR' : if($logLevel=='error') $needLog=true;
			case 'WARN ' : if($logLevel=='warn' ) $needLog=true;
			case 'INFO ' : if($logLevel=='info' ) $needLog=true;
			case 'DEBUG' : if($logLevel=='debug') $needLog=true;
		}
		if($needLog){
			self::log($message,$level);
		}
	}
	
	public static function debug($debug){
		self::tryToLog($debug,'DEBUG');
		self::$debugs[]=Texte::toHtml($debug);
	}
	
	public static function info($info){
		self::tryToLog($info,'INFO ');
		self::$infos[]=Texte::toHtml($info);
	}

	public static function warn($warn){
		self::tryToLog($warn,'WARN ');
		self::$warns[]=Texte::toHtml($warn);
	}

	public static function error($error){
		self::tryToLog($error,'ERROR');
		self::$errors[]=Texte::toHtml($error);
	}

	public static function crash($crash){
		self::tryToLog($crash,'CRASH');
		self::$crashs[]=Texte::toHtml($crash);
	}

	public static function getDebugs(){
		if(!Template::showDebug()) return '';
		return self::toListe(self::$debugs, 'Debug', 'debug');
	}

	public static function getInfos(){
		return self::toListe(self::$infos, 'Information', 'info');
	}

	public static function getWarns(){
		return self::toListe(self::$warns, 'Attention', 'warn');
	}

	public static function getErrors(){
		return self::toListe(self::$errors, 'Erreur', 'error');
	}

	public static function getCrashs(){
		return self::toListe(self::$crashs, 'Erreur critique', 'crash');
	}
	
	private static function toListe($liste, $titre, $classCSS){
		if(count($liste)==0) return '';
		$message="<div class='$classCSS'>$titre : <i>";
		if(count($liste)==1){
			$message.=$liste[0];
		}
		else{
			$message.='<ul><li>';
			$message.=implode('</li><li>',$liste);
			$message.='</li></ul>';
		}
		$message.='</i></div>';
		return $message;
	}
	
	public static function serializeMe(){
		return serialize(array(
			'infos'=>self::$infos,
			'warns'=>self::$warns,
			'errors'=>self::$errors,
			'crashs'=>self::$crashs
		));
	}
	
	public static function unserializeMe($string){
		$values=unserialize($string);
		self::$infos=$values['infos'];
		self::$warns=$values['warns'];
		self::$errors=$values['errors'];
		self::$crashs=$values['crashs'];
	}
	
}

?>